package crl.menu
{
	import caurina.transitions.Tweener;
	
	import crl.menu.data.Config;
	import crl.menu.events.ListItemEvent;
	import crl.menu.view.PageList;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	public class SubMenu extends EventDispatcher
	{
		private var app:Application;
		private var len:int=7;
		
		
		private var skin:MovieClip;
		private var overBackground:OverBackground;
		private var timer:Timer;
		public function SubMenu(app:Application)
		{
			this.app=app;
			
			skin=app.subMenu;
			skin.y=-170;
			
			overBackground=skin.overBg;
			
			skin.addEventListener(MouseEvent.MOUSE_OVER,overHandler);
			skin.addEventListener(MouseEvent.MOUSE_OUT,outHandler);
			
			timer = new Timer(300, 1);
			timer.addEventListener(TimerEvent.TIMER,timerHandler);
			
			
			buildSubMenu();
			
			
			skin.cacheAsBitmap=true;
		}
		
		
		private function buildSubMenu():void{
			var i:int=0;
			var tf:DisplayObject;
			var viewList:PageList;
			var list:Array;
			while (i < len){
				
				list=Config.instance.getList(i);
				
				
				tf=skin["tf"+i] as DisplayObject;
				
				
				if(tf){
					viewList=new PageList();
					viewList.idx=i;
					viewList.display(list,tf);
					
					
					viewList.addEventListener(ListItemEvent.OVER,viewListEventHandler);
					viewList.addEventListener(ListItemEvent.CLICK,viewListEventHandler);
					viewList.addEventListener(ListItemEvent.OUT,viewListEventHandler);
				}
				
				
				i++;
			}
		}
		
		
		private function viewListEventHandler(event:ListItemEvent):void{
			switch(event.type){
				case ListItemEvent.OUT :
					bgOver();
					break;
				case ListItemEvent.OVER :
					bgOver((event.target as PageList).idx);
					break;
				
				case ListItemEvent.CLICK :
					
					
					Config.instance.goto(event.data);
					
					break;
			}
		}
		
		
		private function overHandler(event:Event):void{
			//trace(event.type,event.target)
			open();
		}
		private function outHandler(event:Event):void{
			//trace(event.type,event.target)
			close();
		}
		
		private function timerHandler(event:Event):void{
			Tweener.addTween(skin, {y:-170, time:0.3});
		}
		
		
		private function bgOver(index:int=-1):void{
			
			if(overBackground==null){
				return;
			}
			
			var i:int=0;
			var mc:MovieClip;
			while (i < len)
			{
				mc=overBackground["overBg"+i];
				Tweener.addTween(mc, {alpha:0, time:1});
				i++;
			}
			if (index != -1)
			{
				mc=overBackground["overBg"+index];
				Tweener.addTween(mc, {alpha:1, time:1});
			}
		}
		
		
		public function open(index:int=-1):void{
			
			Tweener.addTween(skin, {y:0, time:0.3});
			timer.stop();
			
			
			if(index !=-1){
				bgOver(index);
			}
		}
		
		public function close(event:Event=null):void{
			timer.start();
			bgOver();
		}
	}
}